package com.yld.car.market;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.Car;
import com.yld.car.domain.FilterCarInfo;

public class FilterTypeActivity extends BaseActivity {

	protected void onCreate(android.os.Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.filter_list);

		utils = NetworkProgressUtils.getInstance();
		list1 = (GridView) findViewById(R.id.list1);
		list2 = (GridView) findViewById(R.id.list2);
		list3 = (GridView) findViewById(R.id.list3);
		confrimBtn = (Button) findViewById(R.id.confrim);
		confrimBtn.setOnClickListener(confrimBtnListener);
		new FilterTask("1", ConstantUtils.GET_CAR_ALL_COLOR_URL, list1)
				.execute("");

		new FilterTask("3", ConstantUtils.GET_CAR_TYPE_OF_GOODS, list2)
				.execute("");
		new FilterTask("2", ConstantUtils.GET_CAR_ALL_PORTS_URL, list3)
				.execute("");
	}

	private Button confrimBtn;
	private GridView list1, list2, list3;
	private NetworkProgressUtils utils;

	private class FilterTask extends AsyncTask<String, String, ArrayList<Car>> {

		public FilterTask(String methodId, String requestURL, GridView list) {
			this.methodId = methodId;
			this.requestURL = requestURL;
			this.list = list;
		}

		private String methodId;
		private String requestURL;
		private GridView list;

		@Override
		protected ArrayList<Car> doInBackground(String... params) {
			//
			// HashMap<String, String> allParams = new HashMap<String,
			// String>();
			// allParams.put("seriesId", params[0]);
			// 20/16
			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(Integer
							.parseInt(methodId)),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(Integer
									.parseInt(methodId)), requestURL, null);

			// System.out.println("type====" + o);
			ArrayList<Car> all = utils.parseJson(o.toString(), "table",
					NetworkProgressUtils.BASE_CAR_TYPE,false);

			return all;
		}

		@Override
		protected void onPostExecute(ArrayList<Car> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			ArrayList<String> all = null;
			if (result != null) {

				all = new ArrayList<String>();
				for (Car b : result) {

					all.add(b.getName());
				}
				// mApp.getAllConfigurations().put("" + methodId, result);
				MyFilterAdapter adapter = new MyFilterAdapter(all, methodId);

				// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				// getApplicationContext(),
				// // android.R.layout.simple_list_item_multiple_choice,
				// android.R.id.text1, all);
				list.setAdapter(adapter);
				// list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

			}
		}
	}

	private class MyFilterAdapter extends BaseAdapter {

		private List<String> listDatas;

		public MyFilterAdapter(List<String> listDatas, String methodId) {
			this.listDatas = listDatas;
			this.methodId = methodId;
		}

		private String methodId;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		private SparseArray<Button> all = new SparseArray<Button>();

		private class ViewHolder {

			public Button button;

		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.filter_list_item, null);
				viewHolder = new ViewHolder();

				// System.out.println("convertView=" + convertView);
				viewHolder.button = (Button) convertView
						.findViewById(R.id.filterBtn);

				convertView.setTag(viewHolder);

			} else {

				viewHolder = (ViewHolder) convertView.getTag();

			}

			final Button btn = viewHolder.button;
			all.put(Integer.valueOf(position), btn);
			// System.out.println(btn);
			btn.setTextColor(getBaseContext().getResources().getColor(
					R.color.blue_config));
			btn.setText(listDatas.get(position));
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					int index = all.indexOfValue(btn);
					btn.setBackgroundResource(R.drawable.selector_btn_01);
					selectedType.put(Integer.parseInt(methodId), btn);
					for (int i = 0; i < all.size(); i++) {
						if (index == i) {
							continue;
						}
						Button b = all.get(i);
						b.setBackgroundResource(R.drawable.selector_btn);

					}

					// for (int i = 0; i < all.size(); i++) {
					//
					// Button b = all.get(i);
					// if (b == v) {
					// b.setBackgroundResource(R.color.white);
					// } else {
					// b.setBackgroundResource(R.drawable.selector_btn);
					// }
					// }
					notifyDataSetChanged();
					// System.out.println("all==" + all.size());

				}
			});
			return convertView;
		}

	}

	private SparseArray<Button> selectedType = new SparseArray<Button>();

	private View.OnClickListener confrimBtnListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			//
			// for (int i = 0; i < selectedType.size(); i++) {
			//
			// System.out.println(selectedType.get(i + 1));
			// }
			Button btn1 = selectedType.get(1);
			Button btn2 = selectedType.get(2);
			Button btn3 = selectedType.get(3);
			String color = null;
			String type = null;
			String port = null;
			if (btn1 == null) {
				color = "";
			} else {
				color = btn1.getText().toString();
			}

			if (btn2 == null) {
				type = "";
			} else {
				type = btn2.getText().toString();
			}
			if (btn3 == null) {
				port = "";
			} else {
				port = btn3.getText().toString();
			}

			// System.out.println("color===" + color + ",,type===" + type
			// + ",,port===" + port);

			Intent intent = new Intent();
			FilterCarInfo f = new FilterCarInfo();
			f.color = color;
			f.type = type;
			f.port = port;
			intent.putExtra("filter", f);

			setResult(RESULT_OK, intent);
			finish();
		}
	};
}
